var module = angular.module('NatureCloud', ['ngAnimate', 'ui.bootstrap', 'ngCookies', 'ui.router', 'chieffancypants.loadingBar', 'ngNotificationsBar', 'ngMaterial', 'ui.codemirror', 'ngDropdowns', 'ngMdIcons', 'angular.ping', 'cgBusy', 'angular-jwt', 'ngMessages', 'duScroll', 'AxelSoft', 'radialIndicator', 'ui.swiper', 'pageslide-directive','ngSanitize']);
module.config(function (/*$cookies,*/ $locationProvider, $stateProvider, $urlRouterProvider, $httpProvider, $mdIconProvider) {


  $httpProvider.interceptors.push('redirectInterceptor');

  //$urlRouterProvider.otherwise('/');
  $urlRouterProvider.otherwise(function ($injector, $location) {
    var path = $location.path();
    if (path.indexOf('/cp/') === 0 ) {
      $location.path('/cp');
    } else
      $location.path("/");
  });

  //route for control panel
  $stateProvider
  //cp is dashboard in future
    .state('cp', {
      url: '/cp',
      views: {
        '': {
          templateUrl: 'template/cp/index.html',
          controller: function ($scope, $location,$cookies) {


            if ($location.path().indexOf("/cp/service")==0) {
              $scope.breadcrumbName = "容器服务";
            } else if ($location.path().indexOf("/cp/build")==0) {
              $scope.breadcrumbName = "代码构建";
            } else if ($location.path().indexOf("/cp/images")==0) {
              $scope.breadcrumbName = "镜像仓库";
            } else if ($location.path().indexOf("/cp/storage")==0) {
              $scope.breadcrumbName = "存储管理";
            } else if ($location.path().indexOf("/cp/config")==0) {
              $scope.breadcrumbName = "配置组管理";
            } else if ($location.path().indexOf("/cp/logManagement")==0) {
              $scope.breadcrumbName = "日志管理";
            }

            $scope.selectBreadcrumb = function (val) {
              $scope.breadcrumbName = val;
            }

              $scope.username = $cookies.get("username");
              $scope.exit = function () {
                $cookies.remove("token");
                var cookies = $cookies.getAll();
                angular.forEach(cookies, function (v, k) {
                  $cookies.remove(k, {path: '/'});
                });

                window.location.href = "/";
              }

          }
        },
        'menu@cp': {
          templateUrl: 'template/cp/menu.html',
          controller: function ($scope, $state, $cookies, jwtHelper, $location) {
            $scope.$state = $state;
            $scope.isShowClusterMenu = false;
            if ($cookies.get("token") !== undefined) {
              var userData = jwtHelper.decodeToken($cookies.get("token"));
              if (userData.info !== undefined && userData.info.isClusterUser === true)
                $scope.isShowClusterMenu = true;
            }
            angular.element(document).ready(function () {
              //for jquery ready
              xenon_custom_init();
            });

            if ($location.path().indexOf("/cp/service")==0) {
              jQuery('#main-menu>li').removeClass('active').eq(0).addClass('active');
            } else if ($location.path().indexOf("/cp/build")==0) {
              jQuery('#main-menu>li').removeClass('active').eq(1).addClass('active');
            } else if ($location.path().indexOf("/cp/images")==0) {
              jQuery('#main-menu>li').removeClass('active').eq(2).addClass('active');

            } else if ($location.path().indexOf("/cp/storage")==0) {
              jQuery('#main-menu>li').removeClass('active').eq(3).addClass('active');

            } else if ($location.path().indexOf("/cp/config")==0) {
              jQuery('#main-menu>li').removeClass('active').eq(4).addClass('active');

            } else if ($location.path().indexOf("/cp/logManagement")==0) {
              jQuery('#main-menu>li').removeClass('active').eq(5).addClass('active');

            }

            jQuery('#main-menu>li').click(function () {
              if (jQuery(this).attr("id") == "note") {
                return;
              }
              $('#main-menu>li').removeClass('active')
              $(this).addClass('active')
            })
            var window_Width=jQuery(window).width();
            if(window_Width<986){
              jQuery(".sidebar-menu").addClass("collapsed")
              jQuery("#navbar").addClass("slideLeft")
              jQuery("#main_con").addClass("slideLeft")
            }
            jQuery(window).resize(function () {

              window_Width=jQuery(window).width();
              if(window_Width<986){
                jQuery(".sidebar-menu").addClass("collapsed")
                jQuery("#navbar").addClass("slideLeft")
                jQuery("#main_con").addClass("slideLeft")
              }
            })
            $scope.showClassMobile=false;
            $scope.showTooltipMobile=function () {
              $scope.showClassMobile=!$scope.showClassMobile;
            }
            $scope.showTooltip = function () {

              $scope.flag = jQuery('.sidebar-menu').is(document.getElementsByClassName("collapsed"));
              $scope.$watch('flag', function () {
                $scope.flag = jQuery('.sidebar-menu').is(document.getElementsByClassName("collapsed"));

              });
              jQuery(".sidebar-menu").addClass("collapsed")
              jQuery("#navbar").addClass("slideLeft")
              jQuery("#main_con").addClass("slideLeft")
              if($scope.flag){
                jQuery(".sidebar-menu").removeClass("collapsed")
                jQuery("#navbar").removeClass("slideLeft")
                jQuery("#main_con").removeClass("slideLeft")

              }
            }
          }
        },
        'main@cp': {
          templateUrl: 'template/cp/main.html',
          controller: function ($scope, $cookies) {
            $scope.username = $cookies.get("username");
            $scope.exit = function () {
              $cookies.remove("token");
              var cookies = $cookies.getAll();
              angular.forEach(cookies, function (v, k) {
                $cookies.remove(k, {path: '/'});
              });

              window.location.href = "/";
            }
          }
        },
        'content@cp': {
          templateUrl: 'template/cp/services.html'
        }
      }
    })
    //sevice list
    .state('cp.service', {
      url: '/service',
      params: {
        clusterId: null
      },
      views: {
        'content@cp': {
          templateUrl: 'template/cp/services.html'
        }
      }
    })
    //service create
    .state('cp.servicecreate', {
      url: '/service/create/:imageName?isMarket',
      params: {"currtab": null},
      views: {
        'content@cp': {
          templateUrl: 'template/cp/serviceCreate.html',
          controller: "ServiceOperatorNavController"
        }
      }
    })
    //service update
    .state('cp.serviceupdate', {
      url: '/service/update/:serviceName',
      params: {
        service: null
      },
      views: {
        'content@cp': {
          templateUrl: 'template/cp/serviceCreate.html',
          controller: "ServiceOperatorNavController"
        }
      }
    })
    //service detail
    .state('cp.servicedetail', {
      url: '/service/detail/:clusterId/:serviceName',
      params: {
        service: null
      },
      views: {
        'content@cp': {
          templateUrl: 'template/cp/serviceDetail.html',
          controller: "ServiceDetailController"
        }
      }
    })
    //image list
    .state('cp.imagelist', {
      url: '/images',
     /* params: {
        type: 'private'
      },*/
      views: {
        'content@cp': {
          templateUrl: 'template/cp/images.html',
          controller: "ImageListController"
        }
      }
    })
    //image detail
    .state('cp.imagedetail', {
      url: '/images/detail/:imageName?isMarket',
      /* params:{
       image:null
       },*/
      caseInsensitiveMatch: false,
      views: {
        'content@cp': {
          templateUrl: 'template/cp/imagedetail.html',
          controller: "ImageDetailController"
        }
      }
    })
    .state('cp.buildlist', {
      url: '/buildlist/:id',
      caseInsensitiveMatch: false,
      params:{
        createRepoInfo: null
      },
      views: {
        'content@cp': {
          templateUrl: 'template/cp/buildList.html'
        }
      }

    })
    .state('cp.buildlistmodal', {
      url: '/buildlist/modal/:type',
      views: {
        'content@cp': {
          templateUrl: 'template/cp/buildList.html'
        }
      },
      onEnter:['$mdDialog','$state',function ($mdDialog,$state) {

        $mdDialog.show({
          controller:'BuildRepoLstController',
          templateUrl: 'template/cp/buildCreate.html',
          parent: angular.element(document.body),
          clickOutsideToClose: true,
          fullscreen: true
        }).then(function ($scope) {
          if ($scope.item) {
            $scope.item.autoGet = true;
            $state.go('cp.buildlist',{createRepoInfo:$scope.item});
          }
        }, function (r) {
          // alertify.error(r);
        });
      }]
    })
    .state('cp.buildhistory', {
      url: '/buildhistory/:runID',
      caseInsensitiveMatch: false,
      views: {
        'content@cp': {
          templateUrl: 'template/cp/buildHistory.html',
          controller: 'BuildHistoryController'
        }
      }
    })
    .state('cp.builddetail', {
      url: '/builddetail/:jobName/:buildSeq',
      /*params:{
       buildInfo: null,
       runInfo: null
       },*/
      caseInsensitiveMatch: false,
      views: {
        'content@cp': {
          templateUrl: 'template/cp/buildDetail.html',
          controller: 'BuildDetailController'
        }
      }
    })
    .state('cp.buildrunningdetail', {
      url: '/buildrunningdetail/:jobName/:buildSeq',
      /*params:{
       buildInfo: null,
       runInfo: null
       },*/
      caseInsensitiveMatch: false,
      views: {
        'content@cp': {
          templateUrl: 'template/cp/buildRunningDetail.html',
          controller: 'BuildRunningDetailController'
        }
      }
    })
    .state('cp.repolist', {
      url: '/repolist?type',
      caseInsensitiveMatch: false,
      views: {
        'content@cp': {
          templateUrl: 'template/cp/repoList.html'
        }
      }
    })
    .state('cp.storagelist', {
      url: '/storagelist',
      caseInsensitiveMatch: false,
      views: {
        'content@cp': {
          templateUrl: 'template/cp/storageList.html',
          controller: "StorageListController"
        }
      }
    })
    .state('cp.filelist', {
      url: '/storagelist/:volId',
      caseInsensitiveMatch: false,
      views: {
        'content@cp': {
          templateUrl: 'template/cp/storageDetailList.html',
          controller: "StorageDetailController"
        }
      }
    })
    .state('cp.clusterlist', {
      url: '/clusters',
      caseInsensitiveMatch: false,
      views: {
        'content@cp': {
          templateUrl: 'template/cp/clusters.html',
          controller: "ClusterListController"
        }
      }
    })
    .state('cp.clusterdetail', {
      url: '/cluster/detail/:clusterId',
      caseInsensitiveMatch: false,
      params: {
        cluster: null
      },
      views: {
        'content@cp': {
          templateUrl: 'template/cp/clusterDetaill.html',
          controller: "ClusterDetailController"
        }
      }
    })
    .state('cp.addclusternode', {
      url: '/cluster/addnode/:clusterId',
      caseInsensitiveMatch: false,
      params: {
        cluster: null
      },
      views: {
        'content@cp': {
          templateUrl: 'template/cp/clusterNodeAdd.html',
          controller: "ClusterAddNodeController"
        }
      }
    })
    .state('cp.clusterplugin', {
      url: '/cluster/plugins/:clusterId',
      caseInsensitiveMatch: false,
      params: {
        cluster: null
      },
      views: {
        'content@cp': {
          templateUrl: 'template/cp/clusterPlugin.html',
          controller: "ClusterPluginController"
        }
      }
    })
    .state('cp.configFileSetList', {
      url: '/configFileSet/list',
      caseInsensitiveMatch: false,
      params: {
        cluster: null
      },
      views: {
        'content@cp': {
          templateUrl: 'template/cp/configFileSetList.html',
          controller: "ConfigFileSetListController"
        }
      }
    })
    .state('cp.addConfigFileSet', {
      url: '/configFileSet/add',
      caseInsensitiveMatch: false,
      params: {
        cluster: null,
        cfgSet:null
      },
      views: {
        'content@cp': {
          templateUrl: 'template/cp/configFileSetCreate.html',
          controller: "ConfigFileSetOperatorNavController"
        }
      }
    })
    .state('cp.editConfigFileSet', {
      url: '/configFileSet/edit/',
      caseInsensitiveMatch: false,
      params: {
        cfs: null
      },
      views: {
        'content@cp': {
          templateUrl: 'template/cp/configFileSetCreate.html',
          controller: "ConfigFileSetOperatorNavController"
        }
      }
    })
    .state('cp.storagemanage', {
      url: '/storagemanage/:storageType/:volname',
      caseInsensitiveMatch: false,
      views: {
        'content@cp': {
          templateUrl: 'template/cp/storageManage.html',
          controller: "StorageManageController"
        }
      }
    })
    .state('cp.getpodlogs', {
      url: '/service/logs/:clusterId/:serviceName',
      caseInsensitiveMatch: false,
      params: {
        svc: null
      },
      views: {
        'content@cp': {
          templateUrl: 'template/cp/selectLogCondition.html',
          controller: "SelectLogConditionController"
        }
      }
    })
    .state('cp.logManagement', {
      url: '/logManagement',
      views: {
        'content@cp': {
          templateUrl: 'template/cp/logManagement.html',
          controller: 'LogManagementController'
        }
      }
    })
  ;
  $locationProvider.html5Mode(true);
//token Interceptor

  //icon
  /* $mdIconProvider
   .iconSet('more_vert', 'img/icons/more_vert.svg', 24)
   .defaultIconSet('img/icons/sets/core-icons.svg', 24);*/

});
